package com.jackrain.nea.fc.cp.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.cp.api.ViewCstoredimQueryCmd;
import com.jackrain.nea.fc.cp.mapper.ViewCstoredimMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import springfox.documentation.spring.web.json.Json;

import java.util.HashMap;
import java.util.List;

/**
 * 店仓属性service层
 *
 * @author wuwenxue
 * @date 2018/01/19
 **/
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "cp")
public class ViewCstoredimQueryCmdImpl extends CommandAdapter implements ViewCstoredimQueryCmd {

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        ViewCstoredimMapper viewCstoredimMapper = ApplicationContextHandle.getBean(ViewCstoredimMapper.class);
        ValueHolder vh = new ValueHolder();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"),
                "yyyy-MM-dd HH:mm:ss"), Feature.OrderedField);
        log.info("ViewCstoredimQueryCmdImpl--param-->{}",param);

        //店仓属性明细ID
        if (null == param || (!param.containsKey("vh"))) {
            throw new NDSException("参数格式错误！");
        }
        JSONObject paramVh = param.getJSONObject("vh");
        if (paramVh == null || !paramVh.containsKey("ret")){
            throw new NDSException("参数格式错误！");
        }

        JSONArray jsonArray = new JSONArray();

        JSONObject ret = paramVh.getJSONObject("ret");
        if (ret == null || !ret.containsKey("CP_C_STOREDIM_ITEM")){
            vh.put("data", jsonArray);
            return vh;
        }
        JSONArray ids= ret.getJSONArray("CP_C_STOREDIM_ITEM");
        if (ids == null || ids.size() <= 0){
            vh.put("data", jsonArray);
            return vh;
        }
        Object[] idArray = ids.toArray();

        List<HashMap> list = viewCstoredimMapper.findAllStoredim(idArray);
        for (HashMap<String, Object> hashMap : list) {
            //属性值可用
            String yes = "Y";
            String no = "N";
            String isactive = TypeUtils.castToString(hashMap.get("ISACTIVE"));
            if (yes.equals(isactive)) {
                hashMap.put("ISACTIVE", 0);
            } else if (no.equals(isactive)) {
                hashMap.put("ISACTIVE", 1);
            }

            JSONObject js = new JSONObject(hashMap);
            JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(js, "yyyy-MM-dd HH:mm:ss",
                    SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            jsonArray.add(param1);

        }
        vh.put("data", jsonArray);

        return vh;
    }
}
